// Tags:
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>

const int N = 1e6 + 5, mod = 998244353;
int n, len, pos[N][26], tot[26];
char str[N], s[N];
std::vector<int> vec[26];

inline void solve() {
  scanf("%s", s);
  int l = strlen(s);
  int j = 0, ans = 0, alpha;
  int ch = s[j];
  int p = 0;
  while (j < l) {
    if (ch == '*') {
      int num = 0;
      while (isdigit(ch = s[++j]) && j < l) {
        num = (num << 1) + (num << 3) + (ch - '0');
        if (num >= mod) num -= mod;
      }

      p += num, ans += num;
      if (p >= l) p %= l;
    } else {
      alpha = ch - 'a';
      if (vec[alpha].empty()) {
        puts("-1");
        return;
      }
      int num = 0, div = 0;
      while (isdigit(ch = s[++j]) && j < l) {
        num = (num << 1) + (num << 3) + (ch - '0');
        while (num >= vec[alpha].size()) num -= vec[alpha].size(), div++;
        if (div >= mod) div %= mod;
      }
      auto iter = std::lower_bound(vec[alpha].begin(), vec[alpha].end(), p) + 1;
      if (iter != vec[alpha].end()) {
        num -= vec[alpha].size() - (iter - vec[alpha].begin()) - 1;
        if (num < 0) num += vec[alpha].size(), div--;
        ans += len - *iter;
      }
      p = vec[alpha][num], ans += div * len;
      ans += vec[alpha][num];
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("T3.in", "r", stdin);
  freopen("T3.out", "w", stdout);
#endif
#endif

  scanf("%s", str);
  len = strlen(str);
  for (int i = 0; i < len; ++i) { vec[str[i] - 'a'].push_back(i); }
  for (int i = 0; i < len; ++i) {
    for (int j = 0; j < 26; ++j) {
      if (vec[j].empty()) continue;
      std::vector<int>::iterator iter =
          std::lower_bound(vec[j].begin(), vec[j].end(), i) + 1;
      if (iter == vec[j].end()) iter = vec[j].begin();
      pos[i][j] = iter - vec[j].begin();
    }
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) { solve(); }
  return 0;
}